using Digitalmes.Application.Mes.Processes.Operations;
using Digitalmes.Application.Mes.Processes.Operations.Commands;
using Digitalmes.Application.Mes.Processes.Operations.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Processes;

/// <summary>
/// 工序终结点
/// </summary>
public sealed class OperationEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/process", "operation")
            .RequireAuthorization()
            .MapGet(GetOperationListAsync, "list")
            .MapGet(GetOperationPagedListAsync, "list-page")
            .MapPost(CreateOperationAsync)
            .MapPut(UpdateOperationAsync, "{id}")
            .MapDelete(DeleteOperationAsync, "{id}");
    }

    [EndpointDescription("查询工序集合")]
    public async Task<IApiResult> GetOperationListAsync(ISender sender, [AsParameters] GetOperationListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询工序")]
    public async Task<IApiResult> GetOperationPagedListAsync(ISender sender, [AsParameters] GetOperationListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建工序")]
    public async Task<IApiResult> CreateOperationAsync(ISender sender, ProcOperationDto dto)
    {
        var result = await sender.Send(new CreateOperationCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新工序")]
    public async Task<IApiResult> UpdateOperationAsync(ISender sender, long id, ProcOperationDto dto)
    {
        var result = await sender.Send(new UpdateOperationCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除工序")]
    public async Task<IApiResult> DeleteOperationAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteOperationCommand(id));
        return result.ToApiResult();
    }
}
